package com.ygqh.baby.service.impl;

import com.foxinmy.weixin4j.util.StringUtil;
import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgUserOpenMapper;
import com.ygqh.baby.po.YgUserOpen;
import com.ygqh.baby.po.YgUserOpenExample;
import com.ygqh.baby.po.YgUserOpenExample.Criteria;
import com.ygqh.baby.service.YgUserOpenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class YgUserOpenServiceImpl implements YgUserOpenService {

	@Autowired
	private YgUserOpenMapper ygUserOpenMapper;

	@Override
	public List<YgUserOpen> find() {
		YgUserOpenExample example = new YgUserOpenExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygUserOpenMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgUserOpenExample example = new YgUserOpenExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygUserOpenMapper.countByExample(example);
	}

	@Override
	public YgUserOpen findById(Long id) {
		return ygUserOpenMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgUserOpen ygUserOpen) {
		return ygUserOpenMapper.insertSelective(ygUserOpen);
	}

	@Override
	public int update(YgUserOpen ygUserOpen) {
		return ygUserOpenMapper.updateByPrimaryKeySelective(ygUserOpen);
	}

	@Override
	public ResultSet<YgUserOpen> search(QueryInfo queryInfo, String q) {
		List<YgUserOpen> list = ygUserOpenMapper.selectSuper(queryInfo, q);
		int count = ygUserOpenMapper.countSuper(q);
		ResultSet<YgUserOpen> resultSet = new ResultSet<YgUserOpen>(count, list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgUserOpenExample example = new YgUserOpenExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgUserOpen record = new YgUserOpen();
		record.setStatus(DataStatus.Delete);

		return ygUserOpenMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(YgUserOpen ygUserOpen) {
		int r = 0;
		if (ygUserOpen.getId() != null) {
			r = this.update(ygUserOpen);
		} else {
			r = this.save(ygUserOpen);
		}
		return r;
	}

	@Override
	public YgUserOpen findByOpenId(String openId) {
		YgUserOpen userOpen = ygUserOpenMapper.selectByOpenId(openId);

		return userOpen;
	}

	@Override
	public YgUserOpen findByScan(String openId, String sourceCode) {
		YgUserOpenExample example = new YgUserOpenExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name()).andOpenIdEqualTo(openId).andFromCodeEqualTo(sourceCode);
		List<YgUserOpen> list = ygUserOpenMapper.selectByExample(example);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public YgUserOpen findBySmallOpenId(String openId) {
		YgUserOpen userOpen = ygUserOpenMapper.selectBySmallOpenId(openId);
		return userOpen;

	}

	@Override
	public Boolean existWxAuthorization(String openId, String fromCode) {
		YgUserOpenExample example = new YgUserOpenExample();
		example.createCriteria().andOpenIdEqualTo(openId).andFromCodeEqualTo(fromCode).andStatusEqualTo(DataStatus.Valid.name()).andPasswordIsNotNull();
		List<YgUserOpen> list = ygUserOpenMapper.selectByExample(example);
		if (list != null && list.size() > 0) {
			return true;
		}
		return false;
	}

	@Override
	public List<YgUserOpen> findScanCountByFromCode(String[] fromCodes, Date startDate, Date endDate, String openId) {
		YgUserOpenExample example = new YgUserOpenExample();
		Criteria criteria = example.createCriteria().andFromCodeIn(Arrays.asList(fromCodes)).andPasswordIsNull();
		if (startDate != null) {
			criteria.andCreateTimeBetween(startDate, endDate);
		}
		if (StringUtil.isNotBlank(openId)) {
			criteria.andOpenIdNotEqualTo(openId);
		}
		example.setOrderByClause("id desc");
		return ygUserOpenMapper.selectByExample(example);
	}

	@Override
	public int countScanCountByFromCode(String[] sourceCodes, Date startDate, Date endDate, String openId) {
		YgUserOpenExample example = new YgUserOpenExample();
		Criteria criteria = example.createCriteria().andFromCodeIn(Arrays.asList(sourceCodes)).andPasswordIsNull();
		if (startDate != null) {
			criteria.andCreateTimeBetween(startDate, endDate);
		}
		if (StringUtil.isNotBlank(openId)) {
			criteria.andOpenIdNotEqualTo(openId);
		}
		return ygUserOpenMapper.countByExample(example);
	}

	@Override
	public Boolean existBySubscribe(String openId) {
		YgUserOpenExample example = new YgUserOpenExample();
		example.createCriteria().andOpenIdEqualTo(openId).andStatusEqualTo(DataStatus.Valid.name()).andPasswordIsNull();
		List<YgUserOpen> list = ygUserOpenMapper.selectByExample(example);
		return list != null && list.size() > 0;
	}
}
